﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sireass.Bl;
using log4net;
using Telerik.Web.UI;
using Sireass.Asp.Util;

namespace Sireass.Asp.Administracion
{
	public partial class SeguridadConsultaUsuarios : System.Web.UI.Page
	{
		private UsuarioList ul = null;
		// Create a logger for use in this class
		private static readonly ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

		#region Page
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				Security.ValidarSeguridad(Response, HttpContext.Current.User.Identity, typeof(UsuarioList));
				CargarUsuarios();
			}
			else
			{
				lblExito.Text = string.Empty;
				lblError.Text = string.Empty;
			}
		}
		#endregion

		#region Botones
		protected void radBtnRegresar_Click(object sender, EventArgs e)
		{			
			//mvwAdministracionUsuarios.ActiveViewIndex = 0;
		}		
		#endregion

		#region Grid
		protected void radGrdListado_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
		{
			try
			{
				string strComando = e.CommandName;
				long idUsuario;
				switch (strComando)
				{
					case "Editar":
						idUsuario = (long)radGrdListado.MasterTableView.Items[e.Item.ItemIndex].GetDataKeyValue("IdUsuario");
						Response.Redirect("~/Administracion/SeguridadUsuarioPerfiles.aspx?idUsuario=" + Server.UrlEncode(idUsuario.ToString()));
						break;
					default: break;
				} //fin switch
			}
			catch (Exception ex)
			{
				lblError.Text = ex.Message;
			}
		}

		protected void radGrdListado_PageIndexChanged(object sender, GridPageChangedEventArgs e)
		{
			CargarUsuarios();
		}

		protected void radGrdListado_PageSizeChanged(object sender, GridPageSizeChangedEventArgs e)
		{
			CargarUsuarios();
		}

		protected void radGrdListado_PreRender(object sender, EventArgs e)
		{
			try
			{
				CheckBox chk;
				string strHabilitado;
				foreach (GridDataItem celda in radGrdListado.Items)
				{
					chk = (CheckBox)celda["Activado"].FindControl("chkHabilitado");
					strHabilitado = celda["Habilitado"].Text;

					if (strHabilitado == "True")
						chk.Checked = true;

					else
						chk.Checked = false;
				}
			}
			catch (Exception ex)
			{
				lblError.Text = ex.Message;
			}
		}
		#endregion

		#region Métodos		
		protected Boolean CargarUsuarios() //para cargar el grid
		{
			try
			{
				ul = UsuarioList.GetUsuarioList();
				radGrdListado.DataSource = ul;
				radGrdListado.DataBind();
				return true;
			}
			catch (Exception ex)
			{
				lblError.Text = ex.Message;
				return false;
			}
		}
		#endregion
	}
}